#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target)
    {
        std::sort(candidates.begin(), candidates.end());
        _combinationSum2(candidates, target, 0, 0);
        return _ans;
    }

private:
    void _combinationSum2(const std::vector<int>& candidates, int target, int cur, int sum)
    {
        if (sum == target)
        {
            _ans.emplace_back(_v);
            return;
        }

        if (sum > target)
        {
            return;
        }

        for (int i = cur; i < candidates.size(); ++i)
        {
            if (i != cur && candidates[i] == candidates[i - 1])
            {
                continue;
            }

            _v.push_back(candidates[i]);
            _combinationSum2(candidates, target, i + 1, sum + candidates[i]);
            _v.pop_back();
        }
    }

    std::vector<std::vector<int>> _ans;
    std::vector<int> _v;
};